﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;

namespace Sustainalytics.Controversies.BusinessLogic
{
    
    public class PlainMongoBl<T>:IPlainBl<T> where T : IEntity
    {
        private readonly IRepositoryCollection<T> _repositoryCollection;

        public PlainMongoBl(IRepositoryCollection<T> repository )
        {
            if(repository==null) throw new ArgumentNullException("repository","The repository used by Bl can not be null");
            _repositoryCollection = repository;
        }

        public virtual bool Drop()
        {
            return _repositoryCollection.Drop();
        }

        public virtual bool Create(T obj)
        {
            return _repositoryCollection.Create(obj);
        }

        public virtual bool CreateBatch(IEnumerable<T> obj)
        {
            return _repositoryCollection.CreateBatch(obj);
        }

        public virtual bool CreateOrUpdate(T obj)
        {
            return _repositoryCollection.CreateOrUpdate(obj);
        }

        public virtual bool CreateOrUpdateBatch(IEnumerable<T> obj)
        {
            return _repositoryCollection.CreateOrUpdateBatch(obj);
        }

        public virtual bool Delete(Guid id)
        {
            return _repositoryCollection.Delete(id);
        }


        public void Delete(IEnumerable<Guid> ids)
        {
            _repositoryCollection.Delete(ids);
        }

        public bool DeleteWhere(Expression<Func<T, bool>> wherePredicate)
        {
            return _repositoryCollection.DeleteWhere(wherePredicate);
        }

        public virtual bool DeleteAll()
        {
            return _repositoryCollection.DeleteAll();
        }

        public virtual T Read(Guid id)
        {
            return _repositoryCollection.Read(id);
        }

        public virtual IList<T> ReadAll()
        {
            return _repositoryCollection.ReadAll();
        }

        public virtual IList<T> ReadAll(string[] includedFields, int? skip = null, int? take = null, Ordering<T> ordering = null, bool useCollation = false)
        {
            return _repositoryCollection.ReadAll(includedFields, skip, take, ordering, useCollation);
        }

        public virtual IList<T> ReadAllWhere(Expression<Func<T, bool>> wherePredicate, int? skip = null, int? take = null)
        {
            return _repositoryCollection.ReadAllWhere(wherePredicate, skip, take);
        }

        public virtual IList<T> ReadAllWhereFieldEqualsValue<TK>(Expression<Func<T, TK>> selectPropertyExpression, TK value, int? skip = null, int? take = null,
            string[] includedFields = null)
        {
            return _repositoryCollection.ReadAllWhereFieldEqualsValue(selectPropertyExpression,value, skip, take, includedFields);
        }

        public virtual IList<T> ReadAllWhereFieldEqualsValue<TK>(string fieldName, TK value, int? skip = null, int? take = null,
            string[] includedFields = null)
        {
            return _repositoryCollection.ReadAllWhereFieldEqualsValue(fieldName, value, skip, take, includedFields);
        }

        public virtual IDictionary<Guid, T> ReadbyIds(IEnumerable<Guid> ids, string[] includedFields = null)
        {
            return _repositoryCollection.ReadbyIds(ids, includedFields);
        }

        public virtual long ReadCount()
        {
            return _repositoryCollection.ReadCount();
        }

        public virtual IList<T> ReadWhere(Expression<Func<T, bool>> wherePredicate, int? skip = null, int? take = null, params Expression<Func<T, object>>[] includeFields)
        {
            return _repositoryCollection.ReadWhere(wherePredicate, skip, take, null, includeFields);
        }

        public virtual bool Update(Guid id, T obj)
        {
            return _repositoryCollection.Update(id, obj);
        }

        public virtual long ReadCount(Expression<Func<T, bool>> wherePredicate)
        {
            return _repositoryCollection.ReadCount(wherePredicate);
        }
    }
}
